<template>
<!--  技师模块 -->
  <el-form
    ref="formEngineerRef"
    :model="formEngineerData"
    :rules="formEngineerRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
    :disabled="disabled"
  >
    <el-table :data="formEngineerData" border show-summary  class="-mt-10px">
<!--      <el-table-column label="序号" type="index" align="center" width="60" />-->
      <el-table-column label="技师姓名" align="center" min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.id`" :rules="formEngineerRules.id" class="mb-0px!">
            <el-select
              v-model="row.id"
              clearable
              filterable
              placeholder="请选择技师"
            >
              <el-option
                v-for="item in engineerList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="提成金额" prop="cost"  align="center"  min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cost`" class="mb-0px!">
            <el-input-number
              v-model="row.cost"
              controls-position="right"
              :min="0"
              :precision="0"
              class="!w-100%"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注"  align="center" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3" v-if="!disabled">
    <el-button @click="handleEngineerAdd" round>+ 添加技师</el-button>
  </el-row>
</template>
<script setup lang="ts">

import {EngineerApi,EngineerVO} from "@/api/erp/engineer/index";
import {erpCountInputFormatter, erpPriceInputFormatter, getSumValue} from "@/utils";

const props = defineProps<{
  items: undefined
  disabled: false
}>()
const formLoading = ref(false) // 表单的加载中
const formEngineerData = ref([])
const formEngineerRules = reactive({
  productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '产品数量不能为空', trigger: 'blur' }]
})
const formEngineerRef = ref([]) // 表单 Ref
const engineerList = ref<EngineerVO[]>([]) // 技师列表

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
})

/** 初始化设置 */
watch(
  () => props.items,
  async (val) => {
    formEngineerData.value = val
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleEngineerAdd = () => {

  const engineerRow = {
    id: undefined,
    name: undefined,
    cost: 0,
    remark: undefined
  }
  formEngineerData.value.push(engineerRow)
}

/** 删除按钮操作 */
const handleDelete = (index: number) => {
  formEngineerData.value.splice(index, 1)
}

/** 合计 */
// const getSummaries = (param) => {
//   const { columns, data } = param
//   const sums: string[] = []
//   columns.forEach((column, index: number) => {
//     if (index === 0) {
//       sums[index] = '技师提成合计'
//       return
//     }
//     if (['cost'].includes(column.property)) {
//       const sum = getSumValue(data.map((item) => Number(item[column.property])))
//       sums[index] =
//         column.property === 'cost' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
//     } else {
//       sums[index] = ''
//     }
//   })
//
//   return sums
// }


/** 表单校验 */
const validate = () => {
  return formEngineerRef.value.validate()
}
defineExpose({ validate })

/** 初始化 */
onMounted(async () => {

  /**
   * 获取技师
   */
  const data = await EngineerApi.simpleList();
  engineerList.value = data;

  // const data = await EngineerApi.getEngineerPage(queryParams);
  //   engineerList.value = data.list;
  // 默认添加一个
  if (formEngineerData.value == null){
    formEngineerData.value = [];
  }
  if (formEngineerData.value.length === 0) {
    handleEngineerAdd()
  }
})
</script>
